﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

using System.Threading;

namespace KSOAPlus
{
    static class Program
    {        
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            LoadResourceDll.RegistDLL();            
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool ret;
            Mutex mutex = new Mutex(true, Application.ProductName, out   ret);
            if (ret)
            {
                if (DateTime.Now > Convert.ToDateTime("2022-12-31"))
                {
                    MessageBox.Show("加载LoadResourceDll时出错\n找不到指定的模块。", "RUNDLL", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    
                    if (args.Length == 0)
                    {
                      Application.Run(new main());
                        //Application.Run(new Scanner());
                        //MessageBox.Show("抱歉，此路不通，请通过KSOA.exe进入\n本程序只能通过主程序调用运行", "错误");
                    }
                    else
                    {
                     
                    // if (islogin(args))
                      //  {
                            Common.username = args[1].ToString();
                            switch (args[0].ToString())
                            {
                                case "main":
                                    Application.Run(new main());
                                    break; 
                                case "config":
                                    Application.Run(new SysConfig());
                                    break;
                                case "KSOAPlusToExcelX1":
                                    Application.Run(new DZHToExcel());
                                    break;
                                case "KSOAPlusToExcelX2":
                                    Application.Run(new SAIMSToExcel());
                                    break; 
                                case "UserInfo":
                                    Application.Run(new UserInfo());
                                    break;
                                case "Scanner":
                                    Application.Run(new Scanner());
                                    break;
                                default:
                                    Application.Run(new main());
                                    //MessageBox.Show(args[0].ToString() + "\r\n KSOA Plus 功能不存在"); 
                                    break;
                          //  }
                        }
                    }
                    
                }
            }
            else
            {
                MessageBox.Show("该程序已经启动", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        public static bool islogin(string[] args)
        {
            MsSqlHelper mssql = new MsSqlHelper();
            bool result = true;
            string kl = mssql.GetString("select top 1 kl from [zhiydoc] where dzyname='" + args[1].ToString() + "'");
            if (string.IsNullOrEmpty(kl))
            {
                MessageBox.Show("登录账号不存在！\r\n(๑°ㅁ°๑)ᵎᵎᵎ");
                result = false;
            }
            else
            {
                if (Common.Decrypt(kl) != args[2].ToString())
                {
                    MessageBox.Show("账号登录密码错误！\r\n┭┮﹏┭┮");
                    result = false;
                }
            }
            return result;
        }
    }
}
